import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_base/src/core/bloc_page/bloc_base_page.dart';

typedef Widget HandleWidgetErrorCode(
    DefaultResponseData responseData, LogicParam errorLogicParam);
typedef Widget HandleWidgetResult(dynamic result);

///BaseWidget提供门户2.0首页各个功能模块公用一个PageBloc的需求
///* 避免由于单个网络请求失败出现整个界面显示错误的情况
///* 需要提供WidgetBloc
///* 可以直接使用，无需继承
class BaseWidget<WB extends WidgetBloc> extends BlocBuilder<WB, WidgetState> {
  BaseWidget({
    Key key,
    @required WB bloc,
    @required HandleWidgetResult handleWidgetResult,
    HandleWidgetErrorCode handleWidgetErrorCode,
    BlocWidgetBuilder<WidgetState> builder,
    BlocBuilderCondition<WidgetState> condition,
  }) : super(
          key: key,
          builder: (BuildContext context, WidgetState state) {
            if (state is WidgetHandleResultState) {
              return handleWidgetResult(state.result);
            }
            if (state is WidgetOffstageState) {
              return Offstage();
            }
            if (state is WidgetLogicState) {
              return LogicPage(state.logicParam);
            }
            if (state is WidgetHandleErrorCodeState) {
              return objNotEmpty(handleWidgetErrorCode)
                  ? handleWidgetErrorCode(
                      state.responseData,
                      state.errorLogicParam,
                    )
                  : LogicPage(state.errorLogicParam);
            }
            if (null != builder) {
              return builder(context, state);
            }
            return Container();
          },
          bloc: bloc,
          condition: condition,
        );
}

abstract class WidgetLoadingInitBloc extends WidgetBloc {
  final LogicParam loadingLogicParam;

  WidgetLoadingInitBloc(this.loadingLogicParam);

  @override
  WidgetState get initialState =>
      WidgetLogicState(logicParam: loadingLogicParam ?? LogicParam.loading());
}

abstract class WidgetOffstageInitBloc extends WidgetBloc {
  @override
  WidgetState get initialState => WidgetOffstageState();
}

abstract class WidgetBloc extends Bloc<WidgetEvent, WidgetState> {
  @override
  Stream<WidgetState> mapEventToState(WidgetEvent event) async* {
    if (event is WidgetOffstageEvent) {
      yield WidgetOffstageState();
    }
    if (event is WidgetLogicEvent) {
      yield WidgetLogicState(logicParam: event.logicParam);
    }
    if (event is WidgetHandleErrorCodeEvent) {
      yield WidgetHandleErrorCodeState(
          responseData: event.responseData,
          errorLogicParam: event.errorLogicParam);
    }
    if (event is WidgetHandleResultEvent) {
      yield WidgetHandleResultState(result: event.result);
    }
  }
}

//-------------------------Event事件-开始-------------------------
class WidgetEvent extends PageEvent {
  WidgetEvent([List props = const []]) : super(props);
}

class WidgetOffstageEvent extends WidgetEvent {}

class WidgetLogicEvent extends WidgetEvent {
  final LogicParam logicParam;

  WidgetLogicEvent({@required this.logicParam}) : super([logicParam]);

  @override
  String toString() => 'widget_logic-${logicParam.type}';
}

class WidgetHandleResultEvent extends WidgetEvent {
  final dynamic result;

  WidgetHandleResultEvent({@required this.result}) : super([result]);
}

class WidgetHandleErrorCodeEvent extends WidgetEvent {
  final LogicParam errorLogicParam;
  final DefaultResponseData responseData;

  WidgetHandleErrorCodeEvent(
      {@required this.responseData, this.errorLogicParam})
      : super([responseData]);
}
//-------------------------Event事件-结束-------------------------

//-------------------------State状态-开始-------------------------

class WidgetState extends PageState {
  WidgetState([List props = const []]) : super(props);
}

class WidgetOffstageState extends WidgetState {}

class WidgetLogicState extends WidgetState {
  final LogicParam logicParam;

  WidgetLogicState({@required this.logicParam}) : super([logicParam]);

  @override
  String toString() => 'widget_logic-${logicParam.type}';
}

class WidgetHandleResultState extends WidgetState {
  final dynamic result;

  WidgetHandleResultState({@required this.result}) : super([result]);
}

class WidgetHandleErrorCodeState extends WidgetState {
  final LogicParam errorLogicParam;
  final DefaultResponseData responseData;

  WidgetHandleErrorCodeState(
      {@required this.responseData, this.errorLogicParam})
      : super([responseData]);
}

//-------------------------State状态-结束-------------------------
